﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DawnOfConquest
{
    class ArmyManagment
    {      
        int rGold;
        int bGold;
        private List<Piece> rArmy;
        private List<Piece> bArmy;

        //boolean values
        private Boolean isRedTurn;
        private Boolean inBuyState;
        public Boolean startBattle;

        //controls
        private KeyboardState keyboard;
        private KeyboardState oldKeyboard;

        //info strings
        private String commands;
        private String title;

        //Fonts
        private SpriteFont infoFont;
        private SpriteFont titleFont;
        private Texture2D background;

        //Sprites
        private Texture2D pikeman;
        private Texture2D swordsman;
        private Texture2D knight;
        private Texture2D archer;
        private Texture2D nomad;
        Song SongBattle;

        public ArmyManagment(ContentManager content, List<Piece> rArmy, List<Piece> bArmy)
        {
            //this.avatar = avatar;
            //army = avatar.getArmy();
            //gold = avatar.getGold();
            this.rArmy = rArmy;
            this.bArmy = bArmy;

            rGold = 300;
            bGold = 300;

            isRedTurn = true;
            startBattle = false;
            inBuyState = true;
            title = "Purchase units: Player 1";
            commands = "Key #      Class    (cost)\n";
            commands += "1       - Pikeman (15)\n";
            commands += "2       - Swordsman (25)\n";
            commands += "3       - Knight (40)\n";
            commands += "4       - Archer (25)\n";
            commands += "5       - Nomad (30)\n";

            //load
            SongBattle = content.Load<Song>("Music/Battle");
            infoFont = content.Load<SpriteFont>("selectFont");
            titleFont = content.Load<SpriteFont>("playerFont");
            background = content.Load<Texture2D>("TerrainSprites/background");

            pikeman = content.Load<Texture2D>("UnitSprites/pike manRED");
            swordsman = content.Load<Texture2D>("UnitSprites/sword manRed");
            knight = content.Load<Texture2D>("UnitSprites/KnightRed");
            archer = content.Load<Texture2D>("UnitSprites/archerr");
            nomad = content.Load<Texture2D>("UnitSprites/horse archerRed");
        }

        //write whose turn it is at top
        //if red buy state, display one of each class with corresponding numbers
        //if red upgrade state display all of red's units
        //same for blue
        public void draw(SpriteBatch spriteBatch, ContentManager content)
        {
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(infoFont, commands, new Vector2(270, 300), Color.White);

            //draw sprites
            int x = 310;
            spriteBatch.Draw(pikeman, new Vector2(x, 370), Color.White);
            spriteBatch.Draw(swordsman, new Vector2(x, 420), Color.White);
            spriteBatch.Draw(knight, new Vector2(x, 470), Color.White);
            spriteBatch.Draw(archer, new Vector2(x, 520), Color.White);
            spriteBatch.Draw(nomad, new Vector2(x, 570), Color.White);
            //draw scrollover info
            if (inBuyState)
            {
                //draw all sprites next to name, buy command and price
                //draw current army at bottom
                if (isRedTurn)
                {
                    spriteBatch.DrawString(titleFont, title, new Vector2(260, 40), Color.Red);
                    spriteBatch.DrawString(infoFont, "Gold: " + rGold, new Vector2(500, 175), Color.Gold);
                    int y = 180;
                    foreach (Piece p in rArmy)
                    {
                        spriteBatch.Draw(p.getSprite(), new Vector2(914, y), Color.White);
                        y += 40;
                    }

                    spriteBatch.DrawString(titleFont, "Press Enter to go to \nPlayer 2 army purchase", new Vector2(147, 800), Color.Wheat);
                }
                else
                {
                    spriteBatch.DrawString(titleFont, title, new Vector2(260, 40), Color.Blue);
                    spriteBatch.DrawString(infoFont, "Gold: " + bGold, new Vector2(500, 175), Color.Gold);
                    int y = 180;
                    foreach (Piece p in bArmy)
                    {
                        spriteBatch.Draw(p.getSprite(), new Vector2(914, y), Color.White);
                        y += 40;
                    }
                    spriteBatch.DrawString(titleFont, "Press Enter to begin battle!", new Vector2(147, 800), Color.Wheat);
                }
                
            }
            else if (!inBuyState)
            {
                //draw current army with price to level up next to each
            }
        }

        //determine state (buy/upgrade)
        //change turns
        //start battle
        public void update(ContentManager content)
        {
            oldKeyboard = keyboard;
            keyboard = Keyboard.GetState();
            
            if (isRedTurn && inBuyState)
            {
                if (keyboard.IsKeyDown(Keys.D1) && !oldKeyboard.IsKeyDown(Keys.D1) && rGold > 14)           //add pikeman
                {
                    rArmy.Add(new Pikeman("RED", content));
                    rGold -= 15;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D2) && !oldKeyboard.IsKeyDown(Keys.D2) && rGold > 24)           //add swordsman
                {
                    rArmy.Add(new Swordsman("RED", content));
                    rGold -= 25;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D3) && !oldKeyboard.IsKeyDown(Keys.D3) && rGold > 39)           //add knight
                {
                    rArmy.Add(new Knight("RED", content));
                    rGold -= 40;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D4) && !oldKeyboard.IsKeyDown(Keys.D4) && rGold > 24)           //add Archer
                {
                    rArmy.Add(new Archer("RED", content));
                    rGold -= 25;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D5) && !oldKeyboard.IsKeyDown(Keys.D5) && rGold > 29)           //add Nomad
                {
                    rArmy.Add(new Nomad("RED", content));
                    rGold -= 30;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D6) && !oldKeyboard.IsKeyDown(Keys.D6) && rGold > 74)           //add mage
                {
                   // rArmy.Add(new Mage("RED", content));
                   // rGold -= 75;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D7) && !oldKeyboard.IsKeyDown(Keys.D7) && rGold > 39)           //add zealot
                {
                    //rArmy.Add(new Zealot("RED", content));
                    //rGold -= 40;                            //subtract gold
                }

                if (keyboard.IsKeyDown(Keys.Enter) && !oldKeyboard.IsKeyDown(Keys.Enter))
                {
                    isRedTurn = false;
                    title = "Purchase Units: Player 2";
                    pikeman = content.Load<Texture2D>("UnitSprites/pike manBlue");
                    swordsman = content.Load<Texture2D>("UnitSprites/sword manBlue");
                    knight = content.Load<Texture2D>("UnitSprites/KnightBlue");
                    archer = content.Load<Texture2D>("UnitSprites/archerb");
                    nomad = content.Load<Texture2D>("UnitSprites/horse archerBlue");
                }
            }
           /* else if (isRedTurn && !inBuyState)
            {
                //if red is done change all display sprites to the blue version using content
            }*/
            else if (!isRedTurn)
            {
                if (keyboard.IsKeyDown(Keys.D1) && !oldKeyboard.IsKeyDown(Keys.D1) && bGold > 14)           //add pikeman
                {
                    bArmy.Add(new Pikeman("BLUE", content));
                    bGold -= 15;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D2) && !oldKeyboard.IsKeyDown(Keys.D2) && bGold > 24)           //add swordsman
                {
                    bArmy.Add(new Swordsman("BLUE", content));
                    bGold -= 25;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D3) && !oldKeyboard.IsKeyDown(Keys.D3) && bGold > 39)           //add knight
                {
                    bArmy.Add(new Knight("BLUE", content));
                    bGold -= 40;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D4) && !oldKeyboard.IsKeyDown(Keys.D4) && bGold > 24)           //add Archer
                {
                    bArmy.Add(new Archer("BLUE", content));
                    bGold -= 25;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D5) && !oldKeyboard.IsKeyDown(Keys.D5) && bGold > 29)           //add Nomad
                {
                    bArmy.Add(new Nomad("BLUE", content));
                    bGold -= 30;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D6) && !oldKeyboard.IsKeyDown(Keys.D6) && bGold > 74)           //add mage
                {
                    // rArmy.Add(new Mage("BLUE", content));
                    // rGold -= 75;                            //subtract gold
                }
                else if (keyboard.IsKeyDown(Keys.D7) && !oldKeyboard.IsKeyDown(Keys.D7) && bGold > 39)           //add zealot
                {
                    //rArmy.Add(new Zealot("BLUE", content));
                    //rGold -= 40;                            //subtract gold
                }

                if (keyboard.IsKeyDown(Keys.Enter) && !oldKeyboard.IsKeyDown(Keys.Enter))
                {
                    MediaPlayer.Play(SongBattle);
                    MediaPlayer.IsRepeating = true;
                    startBattle = true;
                }
            }
            else if (!isRedTurn && !inBuyState)
            {

            }
        }

        #region helper methods

        public void addRedGold(int i)
        {
            rGold += i;
        }

        public void addBlueGold(int i)
        {
            bGold += i;
        }

        public void returnToManage(ContentManager content)
        {
            //rArmy = new List<Piece>();
            //bArmy = new List<Piece>();

            isRedTurn = true;
            bGold += 200;
            rGold += 200;
            startBattle = false;

            //load
            pikeman = content.Load<Texture2D>("UnitSprites/pike manRED");
            swordsman = content.Load<Texture2D>("UnitSprites/sword manRed");
            knight = content.Load<Texture2D>("UnitSprites/KnightRed");
            archer = content.Load<Texture2D>("UnitSprites/archerr");
            nomad = content.Load<Texture2D>("UnitSprites/horse archerRed");
        }

        #endregion
    }
}
